﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RED_NET_Core.BLL;
using RED_NET_Core.Utilty;
using RED_NET_Core.Entity;
namespace RED_NET.Manager_s
{
    public partial class PublicAnnouncement : System.Web.UI.Page
    {
        public static int _uId;
        public void Init()
        {
            if (Session["USER_ID"] == null)
            {
                Response.Redirect("~/ContentPage/Login_2.aspx");
            }
            else
                _uId = (int)Session["USER_ID"];
            LoginBLL _login = new LoginBLL();
            Users _user = new Users();
            DataTable _dt = new DataTable();
            try
            {
                _user = _login.GetMessageOfUser(_uId);
            }
            catch (Exception ex)
            {
               Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
            if(_user.Right!=3)
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            try
            {
               
                _dt = News_BLL.GetAnnouncement(0);
            }
            catch (Exception ex)
            {
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
           GridView_Announcement.DataSource = _dt;
           GridView_Announcement.DataBind();
            
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            Init();

        }
        protected void GridView_Announcement_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_Announcement.PageIndex = e.NewPageIndex;
            Init(); //重新绑定GridView数据的函数
            GridView_Announcement.PageIndex = e.NewPageIndex;

        }
        protected void GridView_Announcement_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[1].Visible = false; //如果想使第1列不可见,则将它的可见性设为false
            }
        }
        protected void GridView_Announcement_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
              
              LinkButton LinkButton_Delete = (LinkButton)e.Row.FindControl("LinkButton_Delete");
              LinkButton_Delete.CommandArgument = e.Row.RowIndex.ToString();

              
            }
        }
        protected void DataList_Announcement_ItemDataBound(object sender, EventArgs e)
        {

        }
        protected void GridView_Announcement_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                int _index = Convert.ToInt32(e.CommandArgument);
                int _Id = Int32.Parse(GridView_Announcement.Rows[_index].Cells[1].Text);
                try
                {
                    News_BLL.DeleteAnnouncement(_Id);
                }
                catch (Exception ex)
                {
                    Response.Redirect("~/ContentPage/Error/PageError.aspx");
                }
                Response.Redirect(Request.Url.ToString());
            }
        }
        protected void Button_Public_Click(object sender, EventArgs e)
        {
            string _content = TextBox_Content.Text.ToString();
            News_BLL.AddAnnouncement(_uId, _content);
            TextBox_Content.Text = "";
            Init();
        }
    }
}
